package com.ffcs.cn.action.base.treeTable;

import com.supermap.desktop.core.utilties.StringUtilities;
import com.supermap.desktop.process.ProcessResources;
import org.jetbrains.userdefine.ui.speedSearch.SimpleColoredComponent;
import org.jetbrains.userdefine.ui.speedSearch.SpeedSearchUtil;
import org.jetbrains.userdefine.ui.speedSearch.TreeSpeedSearch;
import org.pushingpixels.substance.api.renderers.SubstanceDefaultTreeCellRenderer;

import javax.swing.*;
import java.awt.*;

/**
  * 负责渲染第一列，即树节点列，这里根据值长度区分了下，不同的长度用了不同的图片
 */
public class DemoJTreeTableTreeCellRender extends SubstanceDefaultTreeCellRenderer {

	// 设置快速搜索器
	private TreeSpeedSearch treeSpeedSearch;

	public void setTreeSpeedSearch(TreeSpeedSearch treeSpeedSearch) {
		this.treeSpeedSearch = treeSpeedSearch;
	}

	@Override
	public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
		JLabel component = (JLabel) super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
		if (value instanceof TreeTableNode) {
			TreeTableData treeTableData = ((TreeTableNode) value).getTreeTableData();
			component.setText(treeTableData.getValue());
			if (treeTableData.getValue().length() == 3) {
				component.setIcon(ProcessResources.getIcon("/processresources/Variable.svg"));
			} else if (treeTableData.getValue().length() == 2) {
				component.setIcon(ProcessResources.getIcon("/processresources/InputParameter.svg"));
			} else {
				component.setIcon(ProcessResources.getIcon("/processresources/WorkFlow/WorkFlow.png"));
			}
			if (isSupportSpeedSearch() ) {
				// 根据搜索文本框内容高亮搜索匹配的内容，得在设置渲染文本控件内容后去高亮
				String searchText = TreeTableNode.getSearchText();
				if (!StringUtilities.isNullOrEmpty(searchText) && treeSpeedSearch != null && component.getText() != null) {
					SpeedSearchUtil.applySpeedSearchHighlighting(treeSpeedSearch, (SimpleColoredComponent)component, searchText, sel);
				} else {
					SpeedSearchUtil.applySpeedSearchHighlighting(tree, (SimpleColoredComponent)component, true, sel);
				}
			}
		}
		return component;
	}

}
